/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.utils;

import comandaXM.excepcions.ExceptionHibernate;
import comandaXM.excepcions.ExceptionXML;
import comandaXM.utils.format.FormatJTable;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;

/**
 *
 * @author xmarti
 */
public abstract class JPanelTaula extends javax.swing.JPanel {

    private JTableXM taula; 
    private FormatJTable formatTable;
    private Object object ; 
    private String path; 
    public JPanelTaula(String path) {
        initComponents();
        this.path=path; 
        this.taula= new JTableXM() {
            @Override
            public void handleSelectionEvent(ListSelectionEvent e) {
                selectionEvent(e);
            }

            @Override
            public void doubleClick(MouseEvent e) {
                modificar(null); 
                
            }
        };
        this.scrollPane.setViewportView(this.taula);
        setEditable(); 
    }
    
    public abstract void nou(java.awt.event.ActionEvent evt); 
    public abstract void modificar(java.awt.event.ActionEvent evt); 
    public abstract void eliminar(java.awt.event.ActionEvent evt); 
    public abstract void select();
    
    public abstract TableModel getModel() throws ExceptionHibernate;
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pMenu = new javax.swing.JPanel();
        lblTitol = new javax.swing.JLabel();
        cmdNou = new javax.swing.JButton();
        cmdModificar = new javax.swing.JButton();
        cmdEliminar = new javax.swing.JButton();
        scrollPane = new javax.swing.JScrollPane();

        pMenu.setEnabled(false);
        pMenu.setOpaque(false);

        lblTitol.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblTitol.setForeground(java.awt.SystemColor.activeCaption);
        lblTitol.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblTitol.setText("(0 de 0)");

        cmdNou.setIcon(new javax.swing.ImageIcon(getClass().getResource("/comandaXM/images/BotoNouPetit.png"))); // NOI18N
        cmdNou.setToolTipText(TDSLanguageUtils.getMessage("jDialogXM.JButton.new.toolTipText"));
        cmdNou.setFocusable(false);
        cmdNou.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cmdNou.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        cmdNou.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNouActionPerformed(evt);
            }
        });

        cmdModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/comandaXM/images/BotoModificarPetit.png"))); // NOI18N
        cmdModificar.setToolTipText(TDSLanguageUtils.getMessage("jDialogXM.JButton.edit.toolTipText"));
        cmdModificar.setFocusable(false);
        cmdModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cmdModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        cmdModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdModificarActionPerformed(evt);
            }
        });

        cmdEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/comandaXM/images/BotoBorrarPetit.png"))); // NOI18N
        cmdEliminar.setToolTipText(TDSLanguageUtils.getMessage("jDialogXM.JButton.remove.toolTipText"));
        cmdEliminar.setFocusable(false);
        cmdEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cmdEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        cmdEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pMenuLayout = new javax.swing.GroupLayout(pMenu);
        pMenu.setLayout(pMenuLayout);
        pMenuLayout.setHorizontalGroup(
            pMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pMenuLayout.createSequentialGroup()
                .addComponent(cmdNou, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(cmdModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(cmdEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 107, Short.MAX_VALUE)
                .addComponent(lblTitol, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );
        pMenuLayout.setVerticalGroup(
            pMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblTitol, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(cmdNou)
            .addComponent(cmdModificar)
            .addComponent(cmdEliminar)
        );

        scrollPane.setMaximumSize(new java.awt.Dimension(100, 32000));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pMenu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
    }// </editor-fold>//GEN-END:initComponents
    public void setTitol (String titol){
        this.lblTitol.setText(titol);
    } 
    public void setEnabled ( boolean estat){
        this.cmdNou.setEnabled(estat);
        this.setEditable(); 
        this.lblTitol.setEnabled(estat);
        this.taula.setEnabled(estat);
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public FormatJTable getFormatTable() {
        return formatTable;
    }
    public void saveFormat(){
        try {
            this.formatTable.setFormat();
            this.formatTable.saveFormat();           
        }catch (ExceptionXML ex) {
            System.out.println("ExceptionXML");     
        }catch (NullPointerException e){
            System.out.println("Exception SaveFormat. NullPointerException");     
        }
    }
    
    private void cmdNouActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNouActionPerformed
        nou(evt); 
    }//GEN-LAST:event_cmdNouActionPerformed

    private void cmdModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdModificarActionPerformed
        modificar(evt); 
    }//GEN-LAST:event_cmdModificarActionPerformed

    private void cmdEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEliminarActionPerformed
        eliminar(evt); 
    }//GEN-LAST:event_cmdEliminarActionPerformed
    private void setEditable(){
        boolean estat= false ; 
        if (this.taula.getSelectedRow()>=0){
            if (this.object instanceof Object)  estat=true;            
            select(); 
        } 
        this.cmdModificar.setEnabled(estat);
        this.cmdEliminar.setEnabled(estat);
                       
    }
    private void selectionEvent (ListSelectionEvent e) {
        this.object=(Object) this.taula.getValueAt(this.taula.getSelectedRow(),0);         
        this.lblTitol.setText(  (this.taula.getSelectedRow()+1) + " de " + this.taula.getRowCount() );
        setEditable(); 
    }
    public void chargeTable() throws ExceptionHibernate{
        if (this.formatTable==null) {
            try {            
                this.formatTable= new FormatJTable(this.taula,this.path);
            } catch (ExceptionXML ex) {
                
            }
        }
        else {
            this.formatTable.setFormat();
            
        }        
        this.taula.chargeModel(this.getModel());
        setEditable(); 
        if(this.taula.getRowCount()>0){
            this.taula.setRowSelectionInterval(0, 0);
        }
                
        this.lblTitol.setText(this.taula.getRowCount()+ " registres." );
        if (this.formatTable!=null)this.formatTable.chargeFormat();
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdEliminar;
    private javax.swing.JButton cmdModificar;
    private javax.swing.JButton cmdNou;
    private javax.swing.JLabel lblTitol;
    private javax.swing.JPanel pMenu;
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration//GEN-END:variables
}
